1   /*
2    * Copyright (C) 2009 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.google;
18  
19  import static java.util.Arrays.asList;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.collect.ImmutableList;
23  import com.google.common.collect.Lists;
24  import com.google.common.collect.testing.TestCharacterListGenerator;
25  import com.google.common.collect.testing.TestListGenerator;
26  import com.google.common.collect.testing.TestStringListGenerator;
27  import com.google.common.collect.testing.TestUnhashableCollectionGenerator;
28  import com.google.common.collect.testing.UnhashableObject;
29  import com.google.common.primitives.Chars;
30  
31  import java.util.Arrays;
32  import java.util.Collections;
33  import java.util.List;
34  
35  /**
36   * Common generators of different types of lists.
37   *
38   * @author Hayward Chan
39   */
40  @GwtCompatible
41  public final class ListGenerators {
42  
43    private ListGenerators() {}
44  
45    public static class ImmutableListOfGenerator extends TestStringListGenerator {
46      @Override protected List<String> create(String[] elements) {
47        return ImmutableList.copyOf(elements);
48      }
49    }
50  
51    public static class BuilderAddListGenerator extends TestStringListGenerator {
52      @Override protected List<String> create(String[] elements) {
53        ImmutableList.Builder<String> builder = ImmutableList.<String>builder();
54        for (String element : elements) {
55          builder.add(element);
56        }
57        return builder.build();
58      }
59    }
60  
61    public static class BuilderAddAllListGenerator
62        extends TestStringListGenerator {
63      @Override protected List<String> create(String[] elements) {
64        return ImmutableList.<String>builder()
65            .addAll(asList(elements))
66            .build();
67      }
68    }
69  
70    public static class BuilderReversedListGenerator
71        extends TestStringListGenerator {
72      @Override protected List<String> create(String[] elements) {
73        List<String> list = asList(elements);
74        Collections.reverse(list);
75        return ImmutableList.copyOf(list).reverse();
76      }
77    }
78  
79    public static class ImmutableListHeadSubListGenerator
80        extends TestStringListGenerator {
81      @Override protected List<String> create(String[] elements) {
82        String[] suffix = {"f", "g"};
83        String[] all = new String[elements.length + suffix.length];
84        System.arraycopy(elements, 0, all, 0, elements.length);
85        System.arraycopy(suffix, 0, all, elements.length, suffix.length);
86        return ImmutableList.copyOf(all)
87            .subList(0, elements.length);
88      }
89    }
90  
91    public static class ImmutableListTailSubListGenerator
92        extends TestStringListGenerator {
93      @Override protected List<String> create(String[] elements) {
94        String[] prefix = {"f", "g"};
95        String[] all = new String[elements.length + prefix.length];
96        System.arraycopy(prefix, 0, all, 0, 2);
97        System.arraycopy(elements, 0, all, 2, elements.length);
98        return ImmutableList.copyOf(all)
99            .subList(2, elements.length + 2);
100     }
101   }
102 
103   public static class ImmutableListMiddleSubListGenerator
104       extends TestStringListGenerator {
105     @Override protected List<String> create(String[] elements) {
106       String[] prefix = {"f", "g"};
107       String[] suffix = {"h", "i"};
108 
109       String[] all = new String[2 + elements.length + 2];
110       System.arraycopy(prefix, 0, all, 0, 2);
111       System.arraycopy(elements, 0, all, 2, elements.length);
112       System.arraycopy(suffix, 0, all, 2 + elements.length, 2);
113 
114       return ImmutableList.copyOf(all)
115           .subList(2, elements.length + 2);
116     }
117   }
118 
119   public static class CharactersOfStringGenerator
120       extends TestCharacterListGenerator {
121     @Override public List<Character> create(Character[] elements) {
122       char[] chars = Chars.toArray(Arrays.asList(elements));
123       return Lists.charactersOf(String.copyValueOf(chars));
124     }
125   }
126 
127   public static class CharactersOfCharSequenceGenerator
128       extends TestCharacterListGenerator {
129     @Override public List<Character> create(Character[] elements) {
130       char[] chars = Chars.toArray(Arrays.asList(elements));
131       StringBuilder str = new StringBuilder();
132       str.append(chars);
133       return Lists.charactersOf(str);
134     }
135   }
136 
137   private abstract static class TestUnhashableListGenerator
138       extends TestUnhashableCollectionGenerator<List<UnhashableObject>>
139       implements TestListGenerator<UnhashableObject> {
140   }
141 
142   public static class UnhashableElementsImmutableListGenerator
143       extends TestUnhashableListGenerator {
144     @Override
145     public List<UnhashableObject> create(UnhashableObject[] elements) {
146       return ImmutableList.copyOf(elements);
147     }
148   }
149 }